#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <formpermission.h>
#include <formsetsystem.h>
#include <formpzlr.h>
#include <formpzcx.h>
#include <formkmxx.h>
#include <formhsxm.h>
#include <formsetdatabase.h>
#include "formgreport.h"
#include "formpzdr.h"
#include "formjf.h"

#include <QFile>
#include <mysql.h>
#include <QMovie>
#include <jcode.h>
#include <QCoreApplication>
#include <QMessageBox>


void MainWindow::resizeEvent(QResizeEvent *resizeEvent)
{
    QString fileName = QCoreApplication::applicationDirPath();
    fileName+="/border-image.jpg";
    ui->mdiArea->setBackground(QBrush(QPixmap(fileName).scaled(ui->mdiArea->width(),ui->mdiArea->height())));
//以下代码仅为消除警告信息
    int d=resizeEvent->size().width();
    d++;

}
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowState(Qt::WindowMaximized);
    this->setWindowTitle("财务辅助系统 v0.1");
    this->show();
    setPermission(); //设置权限
    //QMessageBox::warning(this,"警告",JCode::GetCPUID());
    QString fileName ="";
    bool s;

    fileName = QCoreApplication::applicationDirPath();
    fileName+="/logo.gif";
    s=JCode::isFileExist(fileName);
    if (s==false){
        QString url="http://jinesc.cn/zb_users/upload/2020/12/202012101607582307549973.gif";
        setNetworkGIF(url);
    }else{
        QMovie *movie = new QMovie(fileName); //path是gif图片的绝对路径
        movie->setScaledSize(ui->labGif->size());
        ui->labGif->setMovie(movie);
        movie->start();
    }
    fileName = QCoreApplication::applicationDirPath();
    fileName+="/border-image.jpg";

    s=JCode::isFileExist(fileName);
    if (s==false){
        QString url="http://jinesc.cn/zb_users/upload/2021/12/FinancialOS.jpg";
        //QMessageBox::warning(this,"警告",url);
        border_image(url);
    }
    //QMessageBox::warning(this,"警告",fileName);
    ui->mdiArea->setBackground(QBrush(QPixmap(fileName).scaled(ui->mdiArea->width(),ui->mdiArea->height())));



    ui->statusbar->setSizeGripEnabled(false);//去掉状态栏右下角的三角
    QString str=QString("用户名：%1  密钥:%2 %3").arg(mySQL::UserName).arg(mySQL::CPUID).arg(mySQL::MACadd);
    ui->statusbar->showMessage(str,20000);//显示临时信息2000ms 前面的正常信息被覆盖 当去掉后一项时，会一直显示
    //ui->statusbar->showMessage(str);//显示临时信息2000ms 前面的正常信息被覆盖 当去掉后一项时，会一直显示
    QLabel *per1 = new QLabel(QString("用户名：%1").arg(mySQL::UserName), this);
    QLabel *per2 = new QLabel(QString("%1 年度：%2").arg(mySQL::SYDW).arg(mySQL::ND), this);
    QLabel *per3 = new QLabel(this);
    per3->setFrameStyle(QFrame::Box|QFrame::Sunken);
    per3->setText(tr("<a href=\"https://jinesc.cn\">风帆小组</a>"));
    per3->setOpenExternalLinks(true);//设置可以打开网站链接
    statusBar()->addPermanentWidget(per1); //现实永久信息
    statusBar()->addPermanentWidget(per2);
    statusBar()->insertPermanentWidget(2, per3);

}
void MainWindow::setNetworkGIF(const QString &GifUrl)
{
   QUrl url(GifUrl);
   QNetworkAccessManager manager;
   QEventLoop loop;
   // qDebug() << "图片下载地址： " << GifUrl;
   QNetworkReply *reply = manager.get(QNetworkRequest(url));
   //请求结束并下载完成后，退出子事件循环
   QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
   //开启子事件循环
   loop.exec();
   QByteArray gifmsg = reply->readAll();
   QString curPath= QCoreApplication::applicationDirPath();//获取当前目录
   QString gifname = QString("%1/logo.gif").arg(curPath);

       QFile file(gifname);
       if(!file.open(QFile::WriteOnly))
       {
           qDebug() << "open file fail";
           exit(1);
       }
       file.write(gifmsg);
       file.close();
   QMovie *movie = new QMovie( gifname); //path是gif图片的绝对路径
   movie->setScaledSize(ui->labGif->size());
   ui->labGif->setMovie(movie);
   movie->start();
}

void MainWindow::border_image(const QString &JpgUrl)
{
   QUrl url(JpgUrl);
   QNetworkAccessManager manager;
   QEventLoop loop;
   // qDebug() << "图片下载地址： " << GifUrl;
   QNetworkReply *reply = manager.get(QNetworkRequest(url));
   //请求结束并下载完成后，退出子事件循环
   QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
   //开启子事件循环
   loop.exec();
   QByteArray gifmsg = reply->readAll();
   QString curPath= QCoreApplication::applicationDirPath();//获取当前目录
   QString gifname = QString("%1/border-image.jpg").arg(curPath);
       QFile file(gifname);
       if(!file.open(QFile::WriteOnly))
       {
           qDebug() << "open file fail";
           exit(1);
       }
       file.write(gifmsg);
       file.close();
}
MainWindow::~MainWindow()
{
    delete ui;
}




void MainWindow::on_actionkmsz_triggered()
{
    Formkmxx * formkmxx =new class Formkmxx;
    formkmxx->show();

}

void MainWindow::on_actionpzsr_triggered()
{
    FormPZLR * formpzlr =new class FormPZLR;
    formpzlr->show();
}

void MainWindow::on_actionhsxm_triggered()
{
    FormHSXM * formHSXM =new class FormHSXM;
    formHSXM->show();
}

void MainWindow::on_actionpzcx_triggered()
{
    FormPZCX * formPZCX =new class FormPZCX;
    formPZCX->show();
    formPZCX->setWindowState(Qt::WindowMaximized);
}

void MainWindow::on_actionUser_triggered()
{
    FormPermission * formUser=new class FormPermission;
    formUser->show();
}

void MainWindow::on_actionPZDR_triggered()
{
    FormPZDR * formPZDR=new class FormPZDR;

    formPZDR->show();
    formPZDR->setWindowState(Qt::WindowMaximized);
}

void MainWindow::on_actionsetDataBase_triggered()
{
    FormSetDataBase * formSet=new class FormSetDataBase;
    formSet->show();
}

void MainWindow::on_actionSetSYS_triggered()
{
    FormSetSystem * formSet=new class FormSetSystem;
    formSet->show();
}


void MainWindow::setPermission(){
    //设置权限 主要用于是否显示
    QString QX=mySQL::Permission;
    //qDebug() << QX;
    if (QX.mid(0,1)=="0"){ //用户设置
        ui->actionUser->setEnabled(false);
    }
    if (QX.mid(1,1)=="0"){ //数据库设置
        ui->actionsetDataBase->setEnabled(false);
    }
    if (QX.mid(2,1)=="0"){ //科目设置
        ui->actionkmsz->setEnabled(false);
    }
    if (QX.mid(3,1)=="0"){ //项目设置
        ui->actionhsxm->setEnabled(false);
    }
    if (QX.mid(4,1)=="0"){ //记账凭证录入
        ui->actionpzsr->setEnabled(false);
    }
    if (QX.mid(5,1)=="0"){ //凭证查询
        ui->actionpzcx->setEnabled(false);
    }
    if (QX.mid(6,1)=="0"){ //凭证导入
        ui->actionPZDR->setEnabled(false);
    }
    if (QX.mid(7,1)=="0"){ //财务系统参数
        ui->actionSetSYS->setEnabled(false);
    }
    if (QX.mid(8,1)=="0"){ //通用报表
        ui->actionreport->setEnabled(false);
    }



}

void MainWindow::on_actionreport_triggered()
{
    FormGreport * formReport=new class FormGreport;
    formReport->show();
    formReport->setWindowState(Qt::WindowMaximized);
}

void MainWindow::on_actionJFGL_triggered()
{
    FormJF * FormJF=new class FormJF;
    FormJF->show();
    FormJF->setWindowState(Qt::WindowMaximized);
}


void MainWindow::on_actionAbout_triggered()
{
    QString str=JCode::AboutMe();
    QMessageBox::warning(this,"警告",str);
}
